package com.rimi.lzhg.service.impl;
import com.rimi.lzhg.dao.MenuMapper;
import com.rimi.lzhg.dao.TodayMenuMapper;
import com.rimi.lzhg.entry.Menu;
import com.rimi.lzhg.entry.MenuExample;
import com.rimi.lzhg.entry.TodayMenu;
import com.rimi.lzhg.entry.TodayMenuExample;
import com.rimi.lzhg.service.TodayMenuService;
import com.rimi.lzhg.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TodayMenuImpl implements TodayMenuService {

    @Autowired
    private TodayMenuMapper todayMenuMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public boolean isHasPeopleReserve(Integer todayMenuId) {
        TodayMenu todayMenu = todayMenuMapper.selectByPrimaryKey(todayMenuId);
        return todayMenuMapper.hasOrder(todayMenu);
    }

    @Override
    public List<TodayMenu> getTodayMenuByDate(Date time){
        if(time==null ){
            return null;
        }
        TodayMenuExample todayMenuExample=new TodayMenuExample();
        todayMenuExample.createCriteria().andTimeEqualTo(time);
        return todayMenuMapper.selectByExample(todayMenuExample);
    }

    @Override
    public List<Menu> getMenuByDate(TodayMenu menu,Integer menuTypeId) {
        //构建查询条件
        TodayMenuExample todayMenuExample=new TodayMenuExample();
        if(menu!=null){
            TodayMenuExample.Criteria criteria = todayMenuExample.createCriteria();
            //根据时间查询
            if(menu.getTime()!=null){
                criteria.andTimeEqualTo(menu.getTime());
            }
            //根据类型查询
            if(menu.getType()!=null){
                criteria.andTypeEqualTo(menu.getType());
            }else{
                //默认查询午餐
                menu.setType(1);
            }
        }
        List<TodayMenu> todayMenus = todayMenuMapper.selectByExample(todayMenuExample);
        if(todayMenus!=null&&todayMenus.size()>0){
            String menuIds=null;
            //获取到‘今日菜单’对象
            for (TodayMenu todayMenu : todayMenus) {
                //获取到对应的多个‘菜单’ID
                if(menuIds!=null){
                    menuIds +=","+todayMenu.getMenuIds();
                }else{
                    menuIds =todayMenu.getMenuIds();
                }
            }
            if(menuIds!=null&&!"".equals(menuIds)){
                //截取获取到所有的菜单ID数组
                String[] ids = menuIds.split(",");
                List strings = Arrays.asList(ids);
                //构建查询条件
                MenuExample menuExample = new MenuExample();
                MenuExample.Criteria criteria = menuExample.createCriteria();
                criteria.andIdIn(strings);
                //根据菜单类型ID查询
                if(menuTypeId!=null){
                    criteria.andTypeIdEqualTo(menuTypeId);
                }
                List<Menu> menus = menuMapper.selectByExample(menuExample);
                return menus;
            }
        }
        return null;
    }

    @Override
    public boolean saveTodayMenu(TodayMenu todayMenu) {
        int insert = todayMenuMapper.insert(todayMenu);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public List getAllTodayMenuTime() {
        TodayMenuExample todayMenuExample=new TodayMenuExample();
        todayMenuExample.setOrderByClause("time desc");
        //大于等于今天
        todayMenuExample.createCriteria().andTimeGreaterThanOrEqualTo(new Date());
        List<TodayMenu> todayMenus = todayMenuMapper.selectByExample(todayMenuExample);
        List<Map<String,Object>> dataList=new ArrayList();
        OK:
        for (TodayMenu todayMenu : todayMenus) {
            String key = DateUtil.dateToString(todayMenu.getTime());
            Map<String,Object> map=new HashMap();
            //去重
            for (Map<String, Object> stringObjectMap : dataList) {
                for (String s : stringObjectMap.keySet()) {
                    if(s.equals(key)){
                        continue OK;
                    }
                }
            }
            map.put(key,DateUtil.dateToWeek(todayMenu.getTime()));
            dataList.add(map);
        }
        return dataList;
    }

    @Override
    public TodayMenu getMenuByDateAndType(TodayMenu todayMenu) {
        if(todayMenu!=null&&todayMenu.getType()==null){
            //默认午餐
            todayMenu.setType(1);
        }
        return todayMenuMapper.selectByTodeyMenu(todayMenu);
    }

    @Override
    public List<TodayMenu> getTodayMenuList(TodayMenu todayMenu) {
        //构建查询条件
        TodayMenuExample todayMenuExample=new TodayMenuExample();
        if(todayMenu!=null){
            TodayMenuExample.Criteria criteria = todayMenuExample.createCriteria();
            //根据时间查询
            if(todayMenu.getTime()!=null){
                criteria.andTimeEqualTo(todayMenu.getTime());
            }
            //根据类型查询
            if(todayMenu.getType()!=null){
                criteria.andTypeEqualTo(todayMenu.getType());
            }
            todayMenuExample.setOrderByClause("time desc");
        }
        return todayMenuMapper.selectByExample(todayMenuExample);
    }

    @Override
    public boolean deleteTodayMenuById(Integer id) {
        int i = todayMenuMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean getTodayMenuByMenuId(Integer id) {
        return todayMenuMapper.hasTodayMenuByMenuId(id);
    }
}
